﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace IupNET
{

    public class TabDef
    {
        public TabDef()
        {

        }

        public TabDef(Control ctrl,string title) {
            Control=ctrl;
            Title=title;
        }

        public TabDef(Control ctrl,Image image) {
            Control=ctrl;
            Image=image;
        }

        public TabDef(Control ctrl, string title, Image image)
        {
            Control = ctrl;
            Image = image;
            Title = title;
        }

        /*public static implicit operator IUPControl(TabDef def) {
            return def.Control;
        }*/

        public Image Image { get; set; }
        public string Title { get; set; }
        public Control Control {get;set;}
    }


    public class Tabs:Control
    {
        public Tabs(params Control[] children)
            : base(IntPtr.Zero)
        {
            

            List<IntPtr> carr = new List<IntPtr>();
            foreach (Control ctrl in children)
                carr.Add(ctrl.cobject);
            carr.Add(IntPtr.Zero);
            Initialize(Iupc.Tabsv(carr.ToArray()));
        }

        public Tabs()
            : base(Iupc.Tabsv(new IntPtr[] { IntPtr.Zero }))
        {

        }

        public Tabs(params TabDef[] children):base(IntPtr.Zero)
        {
            int cnt = 0;

            List<IntPtr> carr = new List<IntPtr>();
            foreach (TabDef tabdef in children)
            {
                if (tabdef.Control != null)
                {
                    carr.Add(tabdef.Control);

                }
            }
            carr.Add(IntPtr.Zero);
            Initialize(Iupc.Tabsv(carr.ToArray()));

            //now go for titles and images
            foreach (TabDef tabdef in children)
            {
                if (tabdef.Control != null)
                {
                  

                    if (tabdef.Title != null)
                    {
                        Iupc.StoreAttribute(cobject, "TABTITLE" + cnt.ToString(), tabdef.Title);
                    }

                    if (tabdef.Image != null)
                    {
                        Iupc.StoreAttribute(cobject, "TABIMAGE" + cnt.ToString(), tabdef.Image.HandleName);
                    }

                    cnt++;
                }
            }

        }

       /* public Control Add(Control ctrl)
        {
            Iupc.Append(cobject, ctrl);
            Iupc.Map(ctrl.cobject);
            Iupc.Refresh(ctrl);
            return ctrl;
        }

        public Control Add(Control ctrl,string tabtitle)
        {
            Iupc.Append(cobject, ctrl);
            SetAttribute("TABTITLE" + (ChildCount - 1).ToString(), tabtitle);
            Iupc.Map(ctrl.cobject);
            Iupc.Refresh(ctrl);
            return ctrl;
        }*/

       

        public bool MultiLine
        {
            get
            {
                return GetAttribute("MULTILINE") == "YES";
            }
            set
            {
                SetAttribute("MULTILINE", value ? "YES" : "NO");
            }
        }

        public Size2i Padding
        {
            //TODO: getting this value cause exception, bug in IUE? chack with a c test and report
            set
            {
                SetAttribute("PADDING", Convert.SizeToString(value));
            }
        }


        public new Control Value
        {
            get
            {
                IntPtr ih=Iupc.GetAttributePtr(cobject,"VALUE_HANDLE");
                return FindControl(ih);
            }
            set
            {
                SetAttribute("VALUE", value.HandleName);
            }
        }


        public int ValuePos
        {
            get
            {
                return Convert.StringToInt(GetAttribute("VALUEPOS"));
            }
            set
            {
                SetAttribute("VALUEPOS", value.ToString());
            }
        }

        public TabPosition TabType
        {
            get
            {
                return Convert.StringToPosition(GetAttribute("TABTYPE"));
            }

            set
            {
                SetAttribute("TABTYPE", Convert.PositionToString(value));
            }
        }

        public void SetTitle(int index,string value)
        {
            SetAttribute("TABTITLE" + index.ToString(), value);
        }

        public void SetImage(int index, Image image)
        {
            SetAttribute("TABIMAGE" + index.ToString(), image.HandleName);
        }

        public Orientation TabOrientation
        {
            get
            {
                if (GetAttribute("TABORIENTATION") == "VERTICAL")
                    return Orientation.Vertical;
                return Orientation.Horizontal;
            }

            set
            {
                if (value == Orientation.Vertical)
                    SetAttribute("TABORIENTATION", "VERTICAL");
                else
                    SetAttribute("TABORIENTATION", "HORIZONTAL");
            }
        }

        public EventCallback TabChangeCB
        {
            get
            {
                return CBHandler.Fetch("TABCHANGE_CB");
            }
            set
            {
                CBHandler.Set("TABCHANGE_CB", value);
            }
        }
        
    }
}
